﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestDomainModelVersioner
    {
        #region Setup/Teardown

        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        [TearDown]
        public virtual void TearDownTest()
        {
        }

        #endregion

        [Test]
        public void Test_UpdateSolution_WhenInheritanceRelationshipHasGuidIdentityValue_ShouldFix()
        {
            //---------------Set up test pack-------------------
            var superClass = TestUtilsDMClass.CreateSavedDMClassWithOID(0);
            var subClass = TestUtilsDMClass.CreateSavedDMClassWithOID(2);
            var inheritanceRelationship = new DMInheritanceRelationship
                {
                    SubClass = subClass,
                    SuperClass = superClass,
                    InheritanceType = ORMapping.ClassTableInheritance
                };
            var identityProperty = subClass.NonOIDProperties[0];
            inheritanceRelationship.Identity = identityProperty.ToString();
            //---------------Assert Precondition----------------
            Assert.IsNull(inheritanceRelationship.IdentityProperty);
            Assert.IsNull(inheritanceRelationship.IdentityPropertyID);
            //---------------Execute Test ----------------------
            DomainModelVersioner.UpdateSolution((DMSolution) subClass.Solution);
            //---------------Test Result -----------------------
            Assert.IsNotNull(inheritanceRelationship.IdentityPropertyID);
            Assert.AreSame(identityProperty, inheritanceRelationship.IdentityProperty);
            Assert.AreSame(identityProperty.PropertyName, inheritanceRelationship.Identity, "This value should remain because it is valid");
        }

        [Test]
        public void Test_UpdateSolution_WhenInheritanceRelationshipHasNameIdentityValue_ShouldSetupIdentityProperty()
        {
            //---------------Set up test pack-------------------
            var superClass = TestUtilsDMClass.CreateSavedDMClassWithOID(0);
            var subClass = TestUtilsDMClass.CreateSavedDMClassWithOID(2);
            var inheritanceRelationship = new DMInheritanceRelationship
            {
                SubClass = subClass,
                SuperClass = superClass,
                InheritanceType = ORMapping.ClassTableInheritance
            };
            var identityProperty = subClass.NonOIDProperties[0];
            inheritanceRelationship.Identity = identityProperty.PropertyName;
            //---------------Assert Precondition----------------
            Assert.IsNull(inheritanceRelationship.IdentityProperty);
            Assert.IsNull(inheritanceRelationship.IdentityPropertyID);
            //---------------Execute Test ----------------------
            DomainModelVersioner.UpdateSolution((DMSolution)subClass.Solution);
            //---------------Test Result -----------------------
            Assert.IsNotNull(inheritanceRelationship.IdentityPropertyID);
            Assert.AreSame(identityProperty, inheritanceRelationship.IdentityProperty);
            Assert.AreSame(identityProperty.PropertyName, inheritanceRelationship.Identity, "This value should remain because it is valid");
        }
    }
}
